using System;
using System.Collections.Generic;
using System.Text;
using System.Drawing.Design;
using System.Windows.Forms;
using System.Security;
using System.Security.Permissions;
using System.Windows.Forms.Design;

namespace AntEater.ComponentModel.BuildElements.Design
{
    /// <summary>
    /// Provides a <c>UITypeEditor</c> for visually selecting the Message Importance of a <c>MessageBuildElement</c>.
    /// </summary>
    public class MessageImportanceTypeEditor:UITypeEditor
    {

        private IWindowsFormsEditorService _editorService;

        /// <summary>
        /// Gets or sets the editor service.
        /// </summary>
        /// <value>The editor service.</value>
        public IWindowsFormsEditorService EditorService {
            get { return _editorService; }
            set { _editorService = value; }
        }

        /// <summary>
        /// Edits the specified object's value using the editor style indicated by the <see cref="M:System.Drawing.Design.UITypeEditor.GetEditStyle"></see> method.
        /// </summary>
        /// <param name="context">An <see cref="T:System.ComponentModel.ITypeDescriptorContext"></see> that can be used to gain additional context information.</param>
        /// <param name="provider">An <see cref="T:System.IServiceProvider"></see> that this editor can use to obtain services.</param>
        /// <param name="value">The object to edit.</param>
        /// <returns>
        /// The new value of the object. If the value of the object has not changed, this should return the same object it was passed.
        /// </returns>
        [PermissionSet(SecurityAction.LinkDemand, Name = "FullTrust")]
        public override object EditValue(System.ComponentModel.ITypeDescriptorContext context, IServiceProvider provider, object value) {

            if (context == null) { throw new ArgumentNullException("context"); }
            if (provider == null) { throw new ArgumentNullException("provider"); }

            string importance = value as string;

            //Can only edit strings 
            if (value == null || importance != null) {
                this.EditorService = provider.GetService(typeof(IWindowsFormsEditorService)) as IWindowsFormsEditorService;

                if (this.EditorService != null) {

                    ListBox itemsListView = new ListBox();

                    itemsListView.BorderStyle = BorderStyle.None;

                    itemsListView.Items.Add("(none)");
                    itemsListView.Items.Add("high");
                    itemsListView.Items.Add("normal");
                    itemsListView.Items.Add("low");

                    if (importance != null) {

                        importance = importance.ToLowerInvariant();

                        if (itemsListView.Items.Contains(importance)) {
                            itemsListView.SelectedIndex = itemsListView.Items.IndexOf(importance);
                        }
                        else {
                            itemsListView.Items.Add(importance);
                            itemsListView.SelectedIndex = 4;
                        }
                    }
                    else {
                        itemsListView.SelectedIndex = 0;
                    }

                    itemsListView.SelectedIndexChanged += new EventHandler(SelectedIndexChanged);

                    this.EditorService.DropDownControl(itemsListView);

                    if (itemsListView.SelectedIndex != -1) {
                        importance = itemsListView.SelectedItem.ToString();

                        if (string.Compare(importance, "(none)") == 0) {
                            value = null;
                        }
                        else {
                            value = importance;
                        }
                    }
                }
            }

            return value;
        }

        /// <summary>
        /// Gets the editor style used by the <see cref="M:System.Drawing.Design.UITypeEditor.EditValue(System.IServiceProvider,System.Object)"></see> method.
        /// </summary>
        /// <param name="context">An <see cref="T:System.ComponentModel.ITypeDescriptorContext"></see> that can be used to gain additional context information.</param>
        /// <returns>
        /// A <see cref="T:System.Drawing.Design.UITypeEditorEditStyle"></see> value that indicates the style of editor used by the <see cref="M:System.Drawing.Design.UITypeEditor.EditValue(System.IServiceProvider,System.Object)"></see> method. If the <see cref="T:System.Drawing.Design.UITypeEditor"></see> does not support this method, then <see cref="M:System.Drawing.Design.UITypeEditor.GetEditStyle"></see> will return <see cref="F:System.Drawing.Design.UITypeEditorEditStyle.None"></see>.
        /// </returns>
        [PermissionSet(SecurityAction.LinkDemand, Name = "FullTrust")]
        public override UITypeEditorEditStyle GetEditStyle(System.ComponentModel.ITypeDescriptorContext context) {
            return UITypeEditorEditStyle.DropDown;
        }

        /// <summary>
        /// Handles the SelectedIIndexChanged Event of the ListBox.
        /// </summary>
        /// <param name="sender">The sender.</param>
        /// <param name="e">The <see cref="T:System.EventArgs"/> instance containing the event data.</param>
        private void SelectedIndexChanged(object sender, EventArgs e) {
            this.EditorService.CloseDropDown();
        }
    }
}
